package org.learn.java.rw;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.RandomAccessFile;
import java.nio.file.Path;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.RandomAccess;

public class RandomRW {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		write("abcdef");
		//path();
	}

	public static void write(String msg) {
		
		StringBuilder path = new StringBuilder();
		StringBuilder rp = new StringBuilder();
		path.append(System.getProperty("user.dir"));
		rp.append( path.substring(0, path.lastIndexOf("\\"))).append(File.separator).append("msgCopy");

		
		File msgSave = new File(rp.toString());
		
		if(!msgSave.exists() && !msgSave.isDirectory()){
			msgSave.mkdir();
		}
		rp.append(File.separator)
				.append(new SimpleDateFormat("yyyyMMdd").format(new Date())
						.toString()).append(".txt");
		StringBuilder message = new StringBuilder();
		message.append("Time:").append(new SimpleDateFormat("yyyy-MM-dd:hh:mm:ss:sss")
			.format(new Date()).toString()).append("\r\n").append("Message:").append(msg).append("\r\n");
		RandomAccessFile accessFile = null;
		try {
			accessFile = new RandomAccessFile(rp.toString(), "rw");
			//设置开始位置
			accessFile.seek(accessFile.length());
			accessFile.write(message.toString().getBytes("utf-8"));
			accessFile.close();
		} catch (Exception e) {
			System.out.println("--文件创建/写入失败--");
			e.printStackTrace();
		}

	}
	public static void path(){
		StringBuilder path = new StringBuilder();
		StringBuilder rp = new StringBuilder();
		path.append(System.getProperty("user.dir"));
		rp.append( path.substring(0, path.lastIndexOf("\\"))).append(File.separator).append("msgCopy");
		System.out.println("path:"+path);
		System.out.println("rp:"+rp);
		
	}

}
